package com.flyxiaozhu.erp.core.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.util.Strings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 签名工具
 */
public class SignatureUtils {
    public static String getSign(Map<String, ?> params, String secret, String secretName) {
        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);

        String signStr = "";
        for (String key : keys) {
            String value = String.valueOf(params.get(key));
            if (Strings.isNotBlank(value)) {
                signStr += key + "=" + String.valueOf(params.get(key)) + "&";
            }
        }

        signStr += secretName + "=" + secret;

        return DigestUtils.md5Hex(signStr).toLowerCase();
    }
}
